
minetest.register_on_joinplayer(function(player)
--    local name = player:get_player_name()
    local playermeta = player:get_meta():to_table()
    for k,v in pairs(ambientfear.baseconfig) do
        local configname = 'ambientfear:'..k
        local configvalue = v
        if player:get_meta():contains(configname) then
            configvalue = player:get_meta():get_float(configname)
        end
        player:get_meta():set_float(configname, configvalue)
    end
end)


minetest.register_on_dieplayer(function(player)
    for k,v in pairs(ambientfear.baseconfig) do
        if string.find(k,'actual') then
            local configname = 'ambientfear:'..k
            player:get_meta():set_float(configname, v)
        end
    end
end)

minetest.register_globalstep(function(dtime)

    ambientfear.time_next_tick = ambientfear.time_next_tick - dtime
    while ambientfear.time_next_tick < 0.0 do
        ambientfear.time_next_tick = ambientfear.time_next_tick + ambientfear.tick_time
        for _,player in ipairs(minetest.get_connected_players()) do
            local playervalues = ambientfear.baseconfig
            for k,v in pairs(playervalues) do
                local configname = 'ambientfear:'..k
                playervalues[k] = player:get_meta():get_float(configname) or v
            end
--            print(dump(playervalues))
            local name = player:get_player_name()
            local pos  = player:get_pos()
            local player_health  = player:get_hp()
            local pos_y = pos.y
            -- the middle of the block with the player's head
            pos.y = math.floor(pos_y) + 1.5
            local node = minetest.get_node(pos)
            local light_now = minetest.get_node_light(pos) or 7
            local temp_now = minetest.get_heat(pos) or 50
            local hashelmet = false
            local hasboots = false
            local arminv = ''
            if(ambientfear.has_3darmor) then
                arminv = minetest.get_inventory({type="detached", name=name.."_armor"}):get_list('armor')
                hashelmet = not arminv[1]:is_empty()
                local helm_name = arminv[1]:to_string()
                if(string.match(helm_name,' '))then
                    print(string.find(helm_name,' '))
                    helm_name = string.sub(helm_name,1,string.find(helm_name,' ')-1)
                    print(helm_name)
                end
                print(dump(minetest.registered_items[helm_name].armor_groups.fleshy))
                hasboots = not arminv[4]:is_empty()
            end
            
            -- sunburn
            if (light_now > playervalues.light_upper) then
                local helm_factor = 1
                if(hashelmet) then
                    local helm_name = arminv[1]:to_string()
                    if(string.match(helm_name,' '))then
                        helm_name = string.sub(helm_name,1,string.find(helm_name,' ')-1)
                    end
                    local helm_fleshy = minetest.registered_items[helm_name].armor_groups.fleshy
                    helm_factor = 2.5 / helm_fleshy
                end
                playervalues.light_actualdamage = playervalues.light_actualdamage + helm_factor * ambientfear.tick_time / math.max(playervalues.light_upperdamage,1)
            end
            -- fear of darkness
            if (light_now < playervalues.light_lower) then
                playervalues.light_actualdamage = playervalues.light_actualdamage + ambientfear.tick_time / math.max(playervalues.light_lowerdamage,1)
            end
            -- recreation of light damage
            if ((light_now < playervalues.light_upper) and (light_now > playervalues.light_lower) and (playervalues.light_actualdamage > 0)) then
                local helm_factor = 1
                if(hashelmet) then
                    local helm_name = arminv[1]:to_string()
                    if(string.match(helm_name,' '))then
                        helm_name = string.sub(helm_name,1,string.find(helm_name,' ')-1)
                    end
                    local helm_fleshy = minetest.registered_items[helm_name].groups.armor_heal
                    helm_factor = (1.5 + helm_fleshy / 12)
                end
                playervalues.light_actualdamage = math.max(0, playervalues.light_actualdamage - helm_factor * ambientfear.tick_time / math.max(playervalues.light_recoverdamage,1))
            end
            while playervalues.light_actualdamage > 1 do
                playervalues.light_actualdamage = playervalues.light_actualdamage - 1
                player:set_hp(player_health - 1, 'fall')
                player_health  = player:get_hp()
            end

            -- heat or cold
            if (((temp_now > playervalues.temperature_upper) or (temp_now < playervalues.temperature_lower))) then
                local temp_damage = playervalues.temperature_upperdamage
                if (temp_now < playervalues.temperature_lower) then
                    temp_damage = playervalues.temperature_lowerdamage
                end
                local boot_factor = 1
                if(hasboots) then
                    local boot_name = arminv[4]:to_string()
                    if(string.match(boot_name,' '))then
                        boot_name = string.sub(boot_name,1,string.find(boot_name,' ')-1)
                    end
                    local boot_fleshy = minetest.registered_items[boot_name].armor_groups.fleshy
                    boot_factor = 2.5 / boot_fleshy
                end
                playervalues.temperature_actualdamage = playervalues.temperature_actualdamage + boot_factor * ambientfear.tick_time / math.max(temp_damage,1)
            end

            -- recreation of temperature damage
            if ((temp_now < playervalues.temperature_upper) and (temp_now > playervalues.temperature_lower) and (playervalues.temperature_actualdamage > 0)) then
                local boot_factor = 1
                if(hasboots) then
                    local boot_name = arminv[4]:to_string()
                    if(string.match(boot_name,' '))then
                        boot_name = string.sub(boot_name,1,string.find(boot_name,' ')-1)
                    end
                    local boot_fleshy = minetest.registered_items[boot_name].groups.armor_heal
                    boot_factor = (1.5 + boot_fleshy / 12) 
                end

                playervalues.temperature_actualdamage = math.max(0, playervalues.temperature_actualdamage - boot_factor * ambientfear.tick_time / math.max(playervalues.temperature_recoverdamage,1))
            end
            while playervalues.temperature_actualdamage > 1 do
                playervalues.temperature_actualdamage = playervalues.temperature_actualdamage - 1
                player:set_hp(player_health - 1, 'fall')
                player_health  = player:get_hp()
            end
            for k,v in pairs(playervalues) do
                local configname = 'ambientfear:'..k
                player:get_meta():set_float(configname,v)
            end
        end
    end
end)
